#!/usr/bin/perl
#
# enko2ko.pl
# print ko-manpage from 'enko' file 
# 
#  'enko' file format is quick&dirty-ly designed by amorette,
# which stands for 'english-korean'. this will not completely
# works. but it will help you concentrating on what-you-do.
#
# bug report: amorettia@gmail.com
#
# ChangeLog:
#   2009-03-12  0.01     created by amorette from 'enko2en.pl' script.
# 
use warnings;
use strict;

our $VERSION = '0.01';

my $rx_linest = qr{^%%(.*)\:(\d+)=+$};
my $rx_linemd = qr{^%%-+$}; 
my $rx_lineed = qr{^%%=+$};

my ( $file ) = @ARGV;

open my $fh, "<", $file or die "File not found";
my @lines = <$fh>;
close $fh;

my @st = grep { $lines[$_] =~ m/$rx_linest/ } 0 .. $#lines;
my @md = grep { $lines[$_] =~ m/$rx_linemd/ } 0 .. $#lines;
my @ed = grep { $lines[$_] =~ m/$rx_lineed/ } 0 .. $#lines;

unless (@st == @md && @md == @ed) {
    print "file format is not correct.\n";
    print "use 'enko2ck.pl' to diagnose\n";
    exit(1);
}

for my $i (0 .. $#st) {
    my ($st, $md, $ed) = ($st[$i], $md[$i], $ed[$i]);

    if ($md + 1 == $ed) {
	next if $st + 1 == $md;
	print $lines[$_] for $st+1..$md-1;
    }
    else {
	print $lines[$_] for $md+1..$ed-1;
    }
}
